package com.zzq.my.jetpacks.jectpackapp.net.login

import androidx.lifecycle.viewModelScope
import com.aisier.architecture.base.BaseViewModel
import com.aisier.architecture.util.SingleLiveEvent
import com.aisier.network.observer.StateLiveData
import com.zzq.my.jetpacks.jectpackapp.bean.LoginInfokt
import kotlinx.coroutines.launch

/**
 * 描述：class describe
 * 作者：zhuangzeqin
 * 时间: 2021/11/5-16:29
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class LoginViewModel : BaseViewModel() {
    //获取数据仓库的repository
    private val repository by lazy { LoginRepo() }

    val userLiveData = StateLiveData<LoginInfokt.Data>()
    val userLiveData2 = SingleLiveEvent<LoginInfokt.Data>()
//    val throwLiveData = SingleLiveEvent<Throwable>()//错误提示语

    /**
     * 登录 Retrofit2.0+协程封装
     */
    fun login(userName: String?, password: String?) {
        viewModelScope.launch {
            repository.login(userName, password).also { userLiveData.value = it }
        }
    }

    /**
     * 登录2 采用的 Rxhttp 请求封装
     */
    fun login2(userName: String?, password: String?) {
//        launch {
//            repository.login2(userName, password).apply {
//                onSuccess {
//                    userLiveData2.value = it
//                }
//                onFailure {
//                    errorLiveData.value = it
//                    //另外一种情况； 页面不需要处理错误的逻辑只是提示一下比如登录密码错误
//                    Logger.d(it.toString())
//                    it.errorMsg.show()//提示错误提示语
//                }
//            }
//        }
    }


}